package cn.i7baozh.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author : baoqi.zhang
 * @date :   2022/4/18 13:53
 */
@Slf4j
@RestController
public class FlowLimitController {

    @SentinelResource("testA")
    @GetMapping("/sentinel/flow/testA")
    public String testA() throws InterruptedException {
        return "-------------testA";
    }
    @SentinelResource("testB")
    @GetMapping("/sentinel/flow/testB")
    public String testB() throws InterruptedException {
        return "-------------testB" + testA();
    }

    @SentinelResource("pring")
    public void print(String from) {
        log.info("from:{}, thread:{}", from, Thread.currentThread().getName());
    }

    @SentinelResource(value = "testHotKey", blockHandler = "del_testHotKey")
    @GetMapping("/sentinel/flow/testHotKey")
    public String testHotKey(@RequestParam(required = false, value = "p1") String p1,
                           @RequestParam(required = false, value = "p2") String p2) {
        log.info("from:{}, thread:{}", p1, p2);
        return "testHotKey" + " " + p1 + " " + p2;
    }

    public String del_testHotKey(String p1, String p2, BlockException exception) {
        return "del_testHotKey_" + " " + p1 + " " + p2;
    }
}
